package com.voto.sunflower.activity.me;

import retrofit.RetrofitError;
import retrofit.client.Response;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.voto.sunflower.R;
import com.voto.sunflower.SunflowerApplication;
import com.voto.sunflower.activity.BaseActivity;
import com.voto.sunflower.model.request.UpdateMe;
import com.voto.sunflower.model.response.UserResponse;
import com.voto.sunflower.retrofit.ClientHttpService;
import com.voto.sunflower.retrofit.NetworkHandler;
import com.voto.sunflower.retrofit.RestError;

public class ModifyGenderActivity extends BaseActivity implements OnClickListener{

	public final static String TAG = "ModifyGenderActivity";
	private ImageView femaleEnable;
	private ImageView maleEnable;
	private Resources rs;
	private String nowState;
	private String maleStr;
	private String femaleStr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_gender);
		rs = getResources();
		maleStr = rs.getString(R.string.personal_info_sex_male);
		femaleStr = rs.getString(R.string.personal_info_sex_female);
		
		initHeadTitle();
		initComponent();
	}
	

	private void initComponent() {
		femaleEnable = (ImageView)findViewById(R.id.enable_female);
		maleEnable = (ImageView)findViewById(R.id.enable_male);
		
		findViewById(R.id.female).setOnClickListener(this);
		findViewById(R.id.male).setOnClickListener(this);
		
		String gender = getIntent().getExtras().getString("gender");
		setGenderState(gender);
		nowState = gender;
	}

	private void initHeadTitle() {
		TextView back = (TextView) findViewById(R.id.back);
		TextView title = (TextView) findViewById(R.id.title);
		TextView other = (TextView) findViewById(R.id.other);
		
		title.setText(rs.getString(R.string.edit_gender));
		back.setOnClickListener(this);
		other.setVisibility(View.VISIBLE);
		other.setText(rs.getString(R.string.save));
		other.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.other:
			updateUserInfo();
			showBlankWaitDialog();
			break;
		case R.id.male:
			setGenderState(maleStr);
			break;
		case R.id.female:
			setGenderState(femaleStr);
			break;
		default:
			break;
		}
	}

	
	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {	
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
	}
	
	private void updateUserInfo() {
		UpdateMe update = new UpdateMe();
		update.setGender(nowState);
		ClientHttpService.getMeService().updateMe(update, new UpdateCallback());
	}

	private void setGenderState(String gender) {
		if(gender.equals(femaleStr)){
			femaleEnable.setVisibility(View.VISIBLE);
			maleEnable.setVisibility(View.INVISIBLE);
			nowState = femaleStr;
		}else{
			femaleEnable.setVisibility(View.INVISIBLE);
			maleEnable.setVisibility(View.VISIBLE);
			nowState = maleStr;
		}
	}
	
	class UpdateCallback extends NetworkHandler<UserResponse> {

	@Override
			public void failure(RetrofitError error) {
				Log.d(TAG , "update gender fail------>");
				dismissDialog();
				if (error.getResponse() != null) {
					int statusCode = error.getResponse().getStatus();
					if (statusCode == 500 || statusCode == 501) {
						final RestError body = (RestError) error.getBodyAs(RestError.class);
						showNetworkErrorDialog(body.errorDetails, null);
					} else if (statusCode == 408) {
						showNetworkErrorDialog(getString(R.string.connect_overtime), null);
					} else {
						showNetworkErrorDialog(error.getMessage(), null);
					}
				}
			}

		@Override
		public void success(UserResponse userResponse, Response arg1) {
			super.success(userResponse, arg1);
			Log.d(TAG , "update gender sucess------>");
			dismissDialog();
			Toast.makeText(getApplicationContext(), "成功修改", Toast.LENGTH_SHORT)
					.show();
			SunflowerApplication.setmUser(userResponse.getUser());
			finish();
		}

	}
}
